package com.avfh.netcard.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.avfh.netcard.util.HttpUtils;
import com.avfh.netcard.util.MobileIotSign;
import com.avfh.netcard.vo.CommonVo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wuhui on 2017/6/22
 */
public class MobileIotService {

    private static final String BASE_URL = "http://120.197.89.173:8081/openapi/router";
    private static final String APP_KEY = "m1dgaoiqm0";
    private static final String SECRET = "99f45885736b93fe4476a6e97feaf321";
    private static final String EC_CODE = "2000471618";

    private static void sign(String method, Map<String, String> params) {
        params.put("appKey", APP_KEY);
        params.put("method", method);
        params.put("v", "2.0");
        params.put("format", "json");
        String sign = MobileIotSign.sign(params, SECRET);
        params.put("sign", sign);
    }

    /**
     * 指定日期EC成员总数查询
     *
     * @param month 月份 yyyyMM
     * @return JSONObject
     */
    public static JSONObject groupMembercountQuery(String month) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("month", month);
        sign("iot.group.membercount.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团成员批量月流量查询
     *
     * @param month  月份 yyyyMM
     * @param pageNo 页码
     * @return JSONObject
     */
    public static JSONObject groupMonthlydatausageQuery(String month, int pageNo) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("month", month);
        params.put("pageNo", String.valueOf(pageNo));
        sign("iot.group.monthlydatausage.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团成员批量费用查询
     *
     * @param month  月份 yyyyMM
     * @param pageNo 页码
     * @return JSONObject
     */
    public static JSONObject groupMemberbillQuery(String month, int pageNo) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("month", month);
        params.put("pageNo", String.valueOf(pageNo));
        sign("iot.group.memberbill.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团成员信息查询
     *
     * @param pageNo 页码
     * @return JSONObject
     */
    public static JSONObject groupMemberInfoQuery(int pageNo) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("pageNo", String.valueOf(pageNo));
        sign("iot.group.memberInfo.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团成员批量流量告警清单查询
     *
     * @param dateStr 日期 yyyyMMdd
     * @param pageNo  页码
     * @return JSONObject
     */
    public static JSONObject groupDailydatausagealarmQuery(String dateStr, int pageNo) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("pageNo", String.valueOf(pageNo));
        params.put("dateStr", dateStr);
        sign("iot.group.dailydatausagealarm.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团成员批量日流量查询
     *
     * @param dateStr 日期 yyyyMMdd
     * @param pageNo  页码
     * @return JSONObject
     */
    public static JSONObject groupDailydatausageQuery(String dateStr, int pageNo) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("pageNo", String.valueOf(pageNo));
        params.put("dateStr", dateStr);
        sign("iot.group.dailydatausage.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团信息查询
     *
     * @return JSONObject
     */
    public static JSONObject groupInfoQuery() {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        sign("iot.group.info.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团总费用查询（历史）
     *
     * @param month 日期 yyyyMMdd
     * @return JSONObject
     */
    public static JSONObject groupBillQuery(String month) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("month", month);
        sign("iot.group.bill.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团产品信息查询
     *
     * @return JSONObject
     */
    public static JSONObject groupProductInfoQuery() {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        sign("iot.group.productInfo.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团成员批量停机告警清单查询
     *
     * @param dateStr 日期 yyyyMMdd
     * @param pageNo  页码
     * @return JSONObject
     */
    public static JSONObject groupDailyalarmQuery(String dateStr, int pageNo) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("pageNo", String.valueOf(pageNo));
        params.put("dateStr", dateStr);
        sign("iot.group.dailyalarm.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 集团余额信息实时查询
     *
     * @param productCode 产品编号
     * @return JSONObject
     * @see #groupProductInfoQuery() productCode来源
     */
    public static JSONObject groupBalanceQuery(String productCode) {
        Map<String, String> params = new HashMap<>();
        params.put("ecCode", EC_CODE);
        params.put("productCode", productCode);
        sign("iot.group.balance.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 终端停开机状态实时查询
     *
     * @param msisdn 手机号码
     * @return JSONObject
     */
    public static JSONObject memberTerminalstatusQuery(String msisdn) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        sign("iot.member.terminalstatus.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员月费用详单查询
     *
     * @param msisdn  手机号码
     * @param dateStr 月份 yyyyMM
     * @return JSONObject
     */
    public static JSONObject memberMonthlybilldetailQuery(String msisdn, String dateStr) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        params.put("dateStr", dateStr);
        sign("iot.member.monthlybilldetail.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 一号多终端副卡激活
     *
     * @return
     */
    public static JSONObject terminalactivateChange() {
        //TODO 文档内无此接口
        return null;
    }

    /**
     * 成员信息查询
     *
     * @param msisdn 手机号码
     * @return JSONObject
     */
    public static JSONObject memberInfoQuery(String msisdn) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        sign("iot.member.info.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员告警信息查询
     *
     * @param msisdn  手机号码
     * @param dateStr 日期 yyyyMMdd
     * @return JSONObject
     */
    public static JSONObject memberAlarmQuery(String msisdn, String dateStr) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        params.put("dateStr", dateStr);
        sign("iot.member.alarm.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员套餐已订购信息实时查询
     *
     * @param msisdn 手机号码
     * @return JSONObject
     */
    public static JSONObject memberPackageQuery(String msisdn) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        sign("iot.member.package.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }


    /**
     * 成员日流量信息查询
     *
     * @param msisdn  手机号码
     * @param dateStr 月份 yyyyMM
     * @return JSONObject
     */
    public static JSONObject memberDailydatausageQuery(String msisdn, String dateStr) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        params.put("dateStr", dateStr);
        sign("iot.member.dailydatausage.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员余额实时查询
     *
     * @param msisdn 手机号码
     * @return JSONObject
     */
    public static JSONObject memberBalanceQuery(String msisdn) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        sign("iot.member.balance.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员剩余流量信息实时查询
     *
     * @param msisdn 手机号码
     * @return JSONObject
     */
    public static JSONObject memberDataplanleftQuery(String msisdn) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        sign("iot.member.dataplanleft.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员号码流量信息实时查询
     *
     * @param msisdn 手机号码
     * @return JSONObject
     */
    public static JSONObject memberDatausageQuery(String msisdn) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        sign("iot.member.datausage.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员月费用查询
     *
     * @param msisdn       手机号码
     * @param startDateStr 开始月份 yyyyMM
     * @param endDateStr   结束月份 yyyyMM
     * @return JSONObject
     */
    public static JSONObject memberMonthlybillQuery(String msisdn, String startDateStr, String endDateStr) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        params.put("startDateStr", startDateStr);
        params.put("endDateStr", endDateStr);
        sign("iot.member.monthlybill.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 故障排查信息实时查询
     *
     * @return JSONObject
     */
    public static JSONObject memberFaultcheckQuery() {
        //TODO 文档内无此接口
        return null;
    }

    /**
     * 成员号码已用流量详情实时查询
     *
     * @return JSONObject
     */
    public static JSONObject memberUseflowinfoQuery() {
        //TODO 文档内无此接口
        return null;
    }

    /**
     * 在线信息实时查询
     *
     * @return JSONObject
     */
    public static JSONObject memberGprsrealsingleQuery() {
        //TODO 文档内无此接口
        return null;
    }

    /**
     * 成员月费用查询
     *
     * @param msisdn       手机号码
     * @param startDateStr 开始月份 yyyyMM
     * @param endDateStr   结束月份 yyyyMM
     * @return JSONObject
     */
    public static JSONObject memberMonthlydatausageQuery(String msisdn, String startDateStr, String endDateStr) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        params.put("startDateStr", startDateStr);
        params.put("endDateStr", endDateStr);
        sign("iot.member.monthlydatausage.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }

    /**
     * 成员月费用查询
     *
     * @param msisdn       手机号码
     * @param startDateStr 开始月份 yyyyMM
     * @param endDateStr   结束月份 yyyyMM
     * @return JSONObject
     */
    public static JSONObject memberMonthPackageQuery(String msisdn, String startDateStr, String endDateStr) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        params.put("startDateStr", startDateStr);
        params.put("endDateStr", endDateStr);
        sign("iot.member.monthlydatausage.query", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }


    /**
     * 套餐叠加
     * @param msisdn       手机号码
     * @param newPackage   叠加的套餐包
     * @return JSONObject
     */
    public static JSONObject additionplan(String msisdn, String newPackage) {
        Map<String, String> params = new HashMap<>();
        params.put("msisdn", msisdn);
        params.put("newPackage", newPackage);
        sign("iot.member.additionplan.change", params);
        String response = HttpUtils.get(BASE_URL, params);
        return JSON.parseObject(response);
    }


    public static void main(String[] args) {
        JSONObject jsonObject = memberMonthPackageQuery("1064819069898", "201706", "201706");
//        JSONObject jsonObject = memberInfoQuery("1064819069898");
        CommonVo params = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<CommonVo>(){});
        System.out.println(jsonObject.get("olMberMmFlows"));
        System.out.println(jsonObject.toJSONString());
    }

}
